package top.wbhzx.utils.code;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

public class MyBase64 {
	public static void main(String[] args) {
		/** 原文 */
		String code1 = "朱玄";
		/** 密文 */
		String code2 = MyBase64.encode(code1);
		/** 解密后 */
		String code3 = MyBase64.decode(code2.getBytes());
		
		System.out.println("加密前：" + code1);
		System.out.println("加密后：" + code2);
		System.out.println("解密后：" + code3);
	}
	public static String encode(String code) {
		String asB64 = null;
		try {
			asB64 = Base64.getEncoder().encodeToString(code.getBytes("utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return asB64;
	}
	
	public static String decode(byte[] asB64) {
		byte[] asBytes = Base64.getDecoder().decode(asB64);
		String code = null;
		try {
			code = new String(asBytes, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return code;
	}
}
